/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Charts module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef QMYDISPLAYDEVICE_H
#define QMYDISPLAYDEVICE_H

#include <QtCharts/QXYSeries>
#include <QtCharts/QBarSeries>
#include <QtCharts/QBarSet>
#include <QLineSeries>
#include <QIODevice>
#include <QTimer>
#include <QThread>
#include <QMutex>
#include <QWaitCondition>

// 工作线程类，用于处理音频数据
class AudioProcessorThread : public QThread
{
    Q_OBJECT

public:
    AudioProcessorThread(QObject* parent = nullptr);
    ~AudioProcessorThread() override;
    
    // 设置处理参数
    void setParameters(const char* data, qint64 size, QLineSeries* series, 
                      QBarSeries* barSeries, QBarSet* barSet,
                      qint64 range, int mode, int chartType);
    
    // 停止线程
    void stop();

signals:
    // 处理完成信号，发送处理后的点集
    void processingFinished(const QVector<QPointF>& points);

protected:
    void run() override;

private:
    QMutex m_mutex;
    QWaitCondition m_condition;
    bool m_stopRequested = false;
    
    // 处理参数
    const char* m_data = nullptr;
    qint64 m_size = 0;
    QLineSeries* m_series = nullptr;
    QBarSeries* m_barSeries = nullptr;
    QBarSet* m_barSet = nullptr;
    qint64 m_range = 0;
    int m_mode = 0;
    int m_chartType = 0;
    
    // 处理PCM格式的数据
    QVector<QPointF> processPCMData(const char* data, qint64 maxSize, QLineSeries* series, qint64 range);
    
    // 处理ATUIM格式的数据
    QVector<QPointF> processATUIMData(const char* data, qint64 maxSize, QLineSeries* series, qint64 range);
};

class QmyDisplayDevice : public QIODevice
{
    Q_OBJECT

public:
    // 音频模式枚举
    enum AudioMode {
        PCM,
        ATUIM
    };
    
    // 图表类型枚举
    enum ChartType {
        LineChart,
        RectChart
    };

    explicit QmyDisplayDevice(QLineSeries* series, QBarSeries* barSeries, QBarSet* barSet, 
                             qint64 pointCount, QObject* parent = nullptr);
    ~QmyDisplayDevice() override;
    
    // 设置音频模式
    void setAudioMode(AudioMode mode);
    
    // 获取当前音频模式
    AudioMode audioMode() const;
    
    // 设置图表类型
    void setChartType(ChartType type);
    
    // 获取当前图表类型
    ChartType chartType() const;

protected:
    // 重写 QIODevice 的 readData 和 writeData 函数
    qint64 readData(char *data, qint64 maxSize) override;
    qint64 writeData(const char *data, qint64 maxSize) override;

signals:
    void updateBlockSize(qint64 blockSize);
    void audioDataAvailable(const QByteArray &data);  // 发送录音数据的信号

private slots:
    void handleDelayedUpdate();
    void handleProcessingFinished(const QVector<QPointF>& points);

private:
    QLineSeries* m_series;
    QBarSeries* m_barSeries;
    QBarSet* m_barSet;
    qint64 m_range;
    QTimer* m_delayTimer;
    qint64 m_lastBlockSize = 0;
    AudioMode m_mode = PCM; // 默认使用PCM模式
    ChartType m_chartType = LineChart; // 默认使用线形图
    AudioProcessorThread* m_processorThread; // 音频处理线程
    QMutex m_dataMutex; // 保护数据访问的互斥锁
    QByteArray m_currentData; // 存储当前需要处理的数据
};


#endif // QMYDISPLAYDEVICE_H
